#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
#include "global.h"
#include "luaUnity.h"



namespace LuaHelp {
	sol::table open_luaUnity(sol::this_state L) {
		sol::state_view lua(L);


		sol::table module=lua.script(R"(
require "lfs"
require "unity"
local debug = require"debug"
local math = require "math"
local mod = {}

-------------------------------------------------------------------------
--排序函数
function mod.sort_table( t, order )
	local fun1 = function (a,b)
		for ii = 1,#order do
			if a[order[ii]] < b[order[ii]] then
				return true;
			end;
			if a[order[ii]] > b[order[ii]] then
				return false;
			end;
		end
		return false;
	end
	table.sort( t, fun1 )
end
-------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------
--文件是否存在，并且能能否读取
function mod.file_exists(name)
   local f=io.open(name,"r")
   if f~=nil then io.close(f) return true else return false end
end
----------------------------------------------------------------------------------------------------------


function mod.listMatchingFilesInDirectory(dir, pattern, callback)
    for file in lfs.dir(dir) do
        if file ~= "." and file ~= ".." then
            local path = dir .. '/' .. file
            local attr = lfs.attributes(path)
            if attr.mode == "file" then
                local polar, angle, phi = string.match(file, pattern)
                if polar and angle then
                    callback(path, polar, angle, phi)
                end
            end
        end
    end
end



return mod;

		)");


		module.set_function("get_full_path", &lua::get_full_path);
		module.set_function("parent_path", &lua::parent_path);
		module.set_function("file_name", &lua::file_name);
		module.set_function("extension", &lua::extension);
		module.set_function("remove", [](const std::string& path){
			lua::remove( lua::utf8_to_local(path) );
		});
		module.set_function("current_path", [](const std::string& path){
					lua::current_path( lua::utf8_to_local(path) );
		});

		module.set_function("mkdir", [](const std::string& path){
			lua::mkdir( lua::utf8_to_local(path) );
		});
		module.set_function("putenv", &lua::put_env);
		module.set_function("python", &lua::python);
		module.set_function("utf8_to_local", &lua::utf8_to_local);
		module.set_function("local_to_utf8", &lua::local_to_utf8);

		module.set_function("file_to_str_table", sol::overload(
											&lua::file2strtable
											,[](const std::string& pstr, sol::this_state L){
												return lua::file2strtable(pstr,"\t",L);
											}
							));
		module.set_function("file_to_num_table", sol::overload(
											[](const std::string& pstr, const std::string& dele,
														sol::this_state L){
												return lua::file2numtable(pstr,dele,L);
											}
											,[](const std::string& pstr, sol::this_state L){
												return lua::file2numtable(pstr,"\t",L);
											}
											,[](const std::string& pstr, const std::string& dele,
													int prel,int postl, sol::this_state L){
												return lua::file2numtable(pstr,"\t",prel,postl,L);
											}
							));
//		module.set_function("sort_table", &lua::sort_table);

		return module;
	}
}



extern "C" int luaopen_luaUnity(lua_State* L)
{
	return sol::stack::call_lua(L, 1, LuaHelp::open_luaUnity );
}

